﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Receipts.DataModel;
using CSP.EFPF.CodeOnly;
using CSP.EFPF.Common;

namespace CSP.Receipts.Business
{
    public class ReceiptManager : BaseRepository<Receipt, ReceiptContext>, IReceiptRepository
    {
        public ReceiptManager(IUnitOfWork<ReceiptContext> uow)
            :base(uow)
        {
        }

        public IEnumerable<Receipt> GetNewReceipts()
        {
            var datum =  DateTime.Now.AddMonths(-1);
            return GetMany(rec => rec.CreationDate >= datum);
        }

        public IEnumerable<Receipt> SearchReceipts(string searchPattern)
        {
            return GetMany(rec => rec.Title.Contains(searchPattern) || rec.ReceiptText.Contains(searchPattern) || rec.Url.Contains(searchPattern));
        }

        public IEnumerable<Receipt> GetReceiptsByUser(int userId)
        {
            return GetMany(rec => rec.Owner.Id == userId).OrderBy(rec => rec.CreationDate);
        }

        public IEnumerable<Receipt> GetReceiptsByUser(string username)
        {
            return GetMany(rec => rec.Owner.Username == username).OrderBy(rec => rec.CreationDate);
        }

        public IEnumerable<Receipt> GetReceiptsByCategory(int categoryId)
        {
            return GetMany(rec => rec.CategoryId == categoryId);
        }
    }
}
